<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload"
               @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="公司">
              <el-select placeholder="请选择" class="ele-block" clearable v-model="where.company_id">
                <el-option v-for="val in companyList" :label="val.name" :value="val.id" :key="val.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="店铺名称:">
              <el-input clearable v-model="where.shop_name" placeholder="请输入店铺名称"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="用户名:">
              <el-input clearable v-model="where.user_name" placeholder="请输入用户名"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="店铺状态">
              <el-select placeholder="请选择" class="ele-block" clearable v-model="where.shop_state">
                <el-option v-for="val in shopStateList" :label="val.value" :value="val.key" :key="val.key"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="年">
              <el-select placeholder="请选择" class="ele-block" clearable v-model="where.year">
                <el-option v-for="val in yearList" :label="val" :value="val" :key="val"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="月">
              <el-select placeholder="请选择" class="ele-block" clearable v-model="where.month">
                <el-option v-for="val in monthList" :label="val" :value="val" :key="val"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="日">
              <el-select placeholder="请选择" class="ele-block" clearable v-model="where.sun">
                <el-option v-for="val in sunList" :label="val" :value="val" :key="val"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="所属部门">
              <treeselect v-model="where.dept_id" :options="deptList" placeholder="请选择所属部门"
                          :defaultExpandLevel="3"
                          :normalizer="(d) => { return { id: d.id, label: d.name, children: d.children || undefined } }"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
                     height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="success"
            icon="el-icon-download"
            class="ele-btn-icon"
            @click="exportExcel"
            v-if="permission.includes('sys:data:export')">导出
          </el-button>
        </template>
        <template slot="shop" slot-scope="{row}">
          {{row.shop_name}}[{{row.shop_num}}]
        </template>
        <template slot="action" slot-scope="{row}">
          <el-link type="primary" icon="el-icon-edit" @click="openEdit(row)">详情
          </el-link>
        </template>

      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <Detail :ID="current" :visible.sync="showEdit" @done="reload"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import Detail from './detail.vue';
import Treeselect from "@riophae/vue-treeselect";
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'SystemData',
  components: {Treeselect, Detail},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/data/list',
      // 表格列配置
      columns: [
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'company_name',
          label: '公司',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 80,
        },
         {
          prop: 'user_name',
          label: '用户',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'dept_name',
          label: '部门',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50
        },
        {
          columnKey: 'shop',
          label: '店铺名称',
          width: 100,
          align: 'center',
          showOverflowTooltip: true,
          slot: 'shop',
        },
        {
          prop: 'score',
          label: '评分',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'level',
          label: '店铺等级',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'sale_money',
          label: '成交金额',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'sale_num',
          label: '成交买家数',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'tg_amount',
          label: '推广金额',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'avg_money',
          label: '客单价',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'completed_order',
          label: '成交订单数',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'refund_money',
          label: '退款金额',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'refund_order',
          label: '退款订单数',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'good_num',
          label: '商品成交数',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'day',
          label: '日期',
          width: 100,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 80,
          align: 'center',
          resizable: false,
          slot: 'action',
        }
      ],
      // 表格搜索条件
      where: {company_id: ''},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false,
      companyList: [],
      yearList: [2024, 2023],
      monthList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      sunList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
      shopStateList: [{'key': 1, 'value': '正常'}, {'key': 0, 'value': '禁用'}],
      // 部门列表
      deptList: [],
    };
  },
  mounted() {
    this.getCompanyList();
    this.getDeptList(); // 查询部门列表
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({page: 1, where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    getCompanyList() {
      this.$http.get("/company/getUserCompanyList")
        .then((res) => {
          if (res.data.code === 0) {
            this.companyList = res.data.data
          } else {
            this.$message.error(res.data.msg);
          }
        })
    },
    /* 导出数据Excel */
    exportExcel() {
      let info = JSON.parse(JSON.stringify(this.where));
      this.$http
        .get("/data/export", info)
        .then((res) => {
          let data = res.data;
          if (data.code == 0) {
            // 下载文件
            window.location.href = data.data
            this.$message({
              type: "success",
              message: "导出成功",
            });
          } else {
            this.$message.error("导出失败");
          }
        })
    },
    /* 显示编辑 */
    openEdit(row) {
      // console.dir(row)
      if (!row) {
        // 添加
        this.current = row;
        this.showEdit = true;
      } else {
        this.current = row.id;
        this.showEdit = true;
      }
    },
    /**
     * 获取部门列表
     */
    getDeptList() {
      this.$http.get('/dept/getDeptList').then(res => {
        if (res.data.code === 0) {
          this.deptList = this.$util.toTreeData(res.data.data, 'id', 'pid');
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped></style>
